projectsparkfandomcom-20200223-history
Variables
=Introduction= One of the most useful aspects of programming languages is the ability to define variables, and Kode is no exception. Basically, variables allow you to store information for future use. Every object in the game comes with lots of predefined variables, but you can create your own variables to track any extra information you want. Some of the defined variables are read only, meaning you can see their value, but cannot change them. Some defined variables are only allow a limited range and will clamp any values not in their range. There are a few different types of variables, and this page will try to give an example of one that already exists for each object, as well as an example of why you might want to add one of your own. The most basic thing you can do with every variable is assign it a value. You do that with the "equals" tile. For example: WHEN once DO variable: strength equals 5 You need to match the different sides of the equals tile, ie. if you have a number on the left, then you need to have a number on the right. You can't (for example) tell a number variable to equal "Jim". You need a Text variable for that. You can create a new variable of any type by going into the values section, and choosing the variable type that you'd like. =Variable Types= Number All games are full of numbers. Some examples in each object would be things like health and maximum health, gravity factor, how high it jumps and how fast it moves. Some things you might want to add are things like mana and maximum mana, experience points and current level. Numbers can either be whole numbers (integers) or float numbers. Check out the math tiles to see what you can do with them, but all the common things (add, subtract, multiply) as well as some fancier useful things (absolute value, round) are available. More information on numbers. Text Text is used for anything that people will read on the screen, but it can also be used for other things. Each page in the brain can be given a name which is a text field, as well as the object's name. You may want a text variable for things like the names of spells or special moves, or for things like speech bubbles. Text can contain any keyboard characters: letters or numbers, as well as most common punctuation like colons, commas, etc... You can add pieces of text together to form a bigger piece of text, which is called concatenation. For example "Hello " plus "World" would make one piece of text that contains "Hello World". You can add most variable types onto a text. Boolean Boolean variables have two possible values, true or false. Anything that can be represented with a yes or no is a good candidate for a boolean variable. Existing boolean variables for each object are things like "on ground", "jumping", or "left mouse button" (which is global, see Scope of Variables below). You might use a boolean variable to tell if an ability has recharged. A useful operation that booleans support is toggle. This changes the current value of the boolean to be the opposite of what it was (true becomes false, false becomes true). The lines: When [] Do toggle variable: Trigger and When[] Do variable: Trigger equals not variable: Trigger do the exact same thing. Object Object variables refer to objects that exist in the game. Each object has a variable named "me" which refers to itself. Objects also have a variable called "creator", which refers to the object that created it (if an object created it). You might want an object variable to track things like an object's target. It is often useful to store an object you create in an object variable. You can say: When [] Do variable: weapon equals create Bow This lets you refer to the bow you've just created later on in the brain to do things like equip it, or change the damage it deals. The important part is that now you can always refer to that specific bow. More information on objects. Object Set Object sets are just a group of one or more objects. Sets that every object has are its attachments, and its inventory. The built in object sets are good enough for most objects. That said, any time you want to refer to objects in a group you can add them to an object set. Object sets start off empty and you can add items to the set by using the by tile. More generally, you can add objects and object sets together to form the object set containing all these objects. You can remove an object with the by tile. You can find objects in the set that match specific criteria using tiles like in front, or specifying the object type. Eg. each of inventory gallery: Arrow will loop through each arrow in your inventory. More information on object sets. Vector Vector variables can be used for a few different things. They are made up of 3 numbers, x, y and z, called their coordinates. They can actually be two different things: vectors or points. A vector is defined by its direction and magnitude ("length"), and can be represented with an arrow. A point is a precise position in space. All objects keep track of a bunch of vectors like directions: up, down, left, right, forward, backward, or their velocity. They also have points like the position and the center. Vectors are useful for pointing objects in different directions. You might also want to use them if you wanted to say shoot multiple arrows at different directions when you shoot, or always have an object at an exact offset from your character. More information on vectors. Brain Brain variables keep track of a brain on the current object. They allow you to access properties of the tracked brain, like the brain name, slot name or channel name, and its variables (see Brain Variable). It also allows you to control the brain by allowing you to change its current page, and know whether it is active. If the brain referenced by the brain variable is removed from your object somehow, the brain variable will be set to nothing. Color You can specify the colour of things you display, or the appearance of your object, and some other things. Each object has got a primary, secondary and tertiary color. You can use a color variable to change the color of a text or an icon displayed on the screen. Boom Camera Settings This is a special variable that keeps multiple camera settings stored: distance, target offset, pitch and yaw. Used only for boom cameras. You can change each setting individually, or use the In-World Boom Camera Editor. Fixed Camera Settings This is a special variable that keeps multiple camera settings stored: distance, target offset, pitch and yaw. Used only for fixed cameras. You can change each setting individually, or use the In-World Fixed Camera Editor. =Variable Defaults= We know that variables can be assigned values, but what happens if you use a variable before it is given a value? In most programming languages this would immediately give an error, but in Project Spark when you ask for a variable, if you haven't yet assigned it a value, it is given a default value. The default value is different for each variable type. Number - Defaults to 0 Boolean - Defaults to 'false' Text - Defaults to text without any characters in it (empty string "") Object - Defaults to 'nothing'* Object set - Defaults to an object set containing 'nothing'* Vector - Defaults to the Zero vector (0, 0, 0) Brain - Defaults to no brain** Color - Defaults to (1, 1, 0.5) in HSL. More information on default color variable. *nothing is actually a tile that you can check for. You can always ask if your variable equal to nothing. It simply means the variable isn't keeping track of an object. Asking if an object set is equal to nothing always returns false, because nothing is not considered as an object set in this case: to check if an object set doesn't contain any object, ask if not set. **there is no specific tile you can compare a brain against to see if it is empty, but checking when not brain works. Note that a brain which doesn't contain any line of Kode is equivalent to no brain. These defaults are especially important when looking at Evaluating when conditions. =Scope of Variables= Object Scope If you do not add the global or campaign tiles or a brain in front of a variable, it is a variable with object scope. Each object has their own variables (like the speed, the position, etc). For example, if you set a number variable "number" to 1 inside the prop A with the kode number equals 1, it will not be set inside a prop B. If you want to access or modify the value of another object's variable, you need to specify the object before the variable, with the in-world picker or with an object variable (eg. picker: prop B number equals 1). Remember that object variables are scoped to an object, and not to a brain. So if you remove the brain, you will still be able to access variables from the brain of other objects, and if you use multiple brains in an object, changing the value of an object variable in one of those brains will change it for every brain on the object. Brain Scope Project Spark allows you to add multiple brains to an object, and each brain tracks its own variables. By specifying the brain that you want before the variable name, you specify that you want to use the variable on that specific brain. This means that you can have several brains on the same object, all running kode with variables that don't interfere with each other. For an in-depth explanation of how brain scope works, check http://forums.projectspark.com/yaf_postst102557.aspx. Global Scope You can add "global" in front of a variable. Such a variable is not specific to an object, it is unique in the world, and it is accessible in every object. Even though there is no "global" tile needed in front of it, position, objects, or teams are good examples of global variables. If you want to make a singleplayer game, it would be a good idea to create a global "score" number variable. Campaign Scope You can add "campaign" in front of a variable. Such a variable is not specific to an object, it is unique in the whole campaign, and it is accessible in every object from every world in that campaign. This is only useful if you are using level linking, in order to store variables and objects that need to stay the same from one world to another. For instance, you want to store your own player as a campaign object variable to be able to create it in each new world you enter. The objects tile is an example of a campaign variable. =Pinning a variable= You can add a variable to "My variables" in the options of a prop, which lets you quickly change its initial value at the start of the game. In order to do that, go inside the brain editor and right click a tile of your variable, click "Info" and then "Pin". =Read Next= When Conditions With Variables =Further Reading= Timing and Logic Brain Tiles < Back to Koding Curriculum fr:Variables Category:Brains